﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;
using PloobsEngine.Modelo;
using PloobsEngine.SceneControl;
using PloobsEngine.Cameras;
using PloobsEngine.Modelo.Animation;

namespace PloobsEngine.Material
{
    /// <summary>
    /// Should BE NOT USED
    /// UNDER CONSTRUCTION YET
    /// </summary>
    internal class BilboardForward : IShader
    {
        private Effect _shader;               

        public BilboardForward()
        {          
        }
        VertexDeclaration declVertices = null;
        VertexPositionTexture[] propVertices = null;
        short[] triangulos = null;
        private  float fTamImpostor = 200;
        private Vector3 pos;

        public Vector3 Position
        {
            get { return pos; }
            set { pos = value; }
        }
        private Texture2D tex;

        public Texture2D Texture
        {
            get { return tex; }
            set { tex = value; }
        }

        public override MaterialType MaterialType
        {
            get { return MaterialType.FORWARD; }
        }
        
        public override void Draw(IModelo modelo, IRenderHelper render, ICamera camera)
        {
            
            Matrix world = Matrix.CreateConstrainedBillboard(pos, camera.Position, Vector3.Up, Vector3.Forward, null);

            EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = true;
            _shader.Parameters["TexturaSprite"].SetValue(tex);
            _shader.Parameters["World"].SetValue(world);
            _shader.Parameters["View"].SetValue(camera.View);
            _shader.Parameters["Projection"].SetValue(camera.Projection);

            _shader.Begin();
            _shader.CurrentTechnique.Passes[0].Begin();
            
            EngineStuff.GraphicsDevice.VertexDeclaration = declVertices;
            EngineStuff.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>
                (PrimitiveType.TriangleList, propVertices, 0, 4, triangulos, 0, 2);
            
            _shader.CurrentTechnique.Passes[0].End();

            _shader.End();

            EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = false;
             
        }
        
        public override void Initialize()
        {
            base.Initialize();
            this._shader = EngineStuff.CustomContentManager.GetAsset<Effect>("..\\Content\\Effects\\Bilboard");
            //this._shader = EngineStuff.InternalContentManager.GetAsset<Effect>("..\\Content\\Effects\\RenderGBuffer");            

            declVertices = new VertexDeclaration(EngineStuff.GraphicsDevice,
                VertexPositionTexture.VertexElements);

            propVertices = new VertexPositionTexture[]
                {
                    new VertexPositionTexture(
                        Vector3.Zero,
                        new Vector2(1,1)),
                    new VertexPositionTexture(
                        Vector3.Zero,
                        new Vector2(0,1)),
                    new VertexPositionTexture(
                        Vector3.Zero,
                        new Vector2(0,0)),
                    new VertexPositionTexture(
                        Vector3.Zero,
                        new Vector2(1,0))
                };

            triangulos = new short[] { 0, 1, 2, 2, 3, 0 };

            propVertices[0].Position.X = -fTamImpostor / 2.0f;
            propVertices[0].Position.Y = 0.0f;

            propVertices[1].Position.X = fTamImpostor / 2.0f;
            propVertices[1].Position.Y = 0.0f;

            propVertices[2].Position.X = fTamImpostor / 2.0f;
            propVertices[2].Position.Y = fTamImpostor;

            propVertices[3].Position.X = -fTamImpostor / 2.0f;
            propVertices[3].Position.Y = fTamImpostor;

            propVertices[0].Position.Z = propVertices[1].Position.Z = propVertices[2].Position.Z = propVertices[3].Position.Z = 0.0f;



        }        
    }
}
